//
//  ClientKBTest.cpp
//  GRE
//
//  Created by Kutlak Roman on 24/01/2012.
//  Copyright (c) 2012 University of Aberdeen. All rights reserved.
//

#include <iostream>
#include <iterator>
#include <gtest/gtest.h>

#include <thread>
#include <future>
#include <chrono>

#include "KBServer.h"
#include "ClientKB.h"
#include "Ontology.h"
using namespace kutlak::roman::gre;

class ServerTest : public ::testing::Test {
protected:
    virtual void SetUp()
    {
        mpServer = std::unique_ptr<KBServer>
        (new KBServer("/Users/roman/Projects/NLP/gre/resources/example.nt"));
        
        mpThread = std::unique_ptr<std::thread>
        (new std::thread([this]()
        {
            this->mpServer->Run();
        }));
    }
    
    virtual void TearDown()
    {
        this->mpServer->Exit();
        mpThread->join();
    }
    
    std::unique_ptr<KBServer> mpServer;
    std::unique_ptr<std::thread> mpThread;
    
};

TEST_F(ServerTest, BigRead)
{    
    std::this_thread::sleep_for(std::chrono::seconds(2));
    ClientKB client;
    
    Property filter(Ontology::kType, "furniture");
    std::vector<EntityPtrT> entities = client.Entities(filter);
    
#ifdef DEBUG
    if (!entities.empty())
        std::cout << entities.front()->ToString() << '\n';
    else
        std::cout << "No entities with property: " << filter.ToString() << '\n';
#endif
    
    EXPECT_EQ(5, entities.size());
    
    entities = client.Entities(Property(Ontology::kType, "teapot"));
    
#ifdef DEBUG
    if (!entities.empty())
        std::cout << entities.front()->ToString() << '\n';
#endif
    
    EXPECT_EQ(0, entities.size());
}
